package com.ruoyi.vote.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.admin.domain.bo.VoteFeedbackBo;
import com.ruoyi.vote.admin.domain.vo.VoteFeedbackVo;
import com.ruoyi.vote.admin.domain.VoteFeedback;
import com.ruoyi.vote.admin.mapper.VoteFeedbackMapper;
import com.ruoyi.vote.admin.service.IVoteFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 问题反馈Service业务层处理
 *
 * @author 筑巢
 * @date 2023-07-18
 */
@RequiredArgsConstructor
@Service
public class VoteFeedbackServiceImpl implements IVoteFeedbackService {

    private final VoteFeedbackMapper baseMapper;

    /**
     * 查询问题反馈
     */
    @Override
    public VoteFeedbackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询问题反馈列表
     */
    @Override
    public TableDataInfo<VoteFeedbackVo> queryPageList(VoteFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VoteFeedback> lqw = buildQueryWrapper(bo);
        Page<VoteFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问题反馈列表
     */
    @Override
    public List<VoteFeedbackVo> queryList(VoteFeedbackBo bo) {
        LambdaQueryWrapper<VoteFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VoteFeedback> buildQueryWrapper(VoteFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VoteFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), VoteFeedback::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增问题反馈
     */
    @Override
    public Boolean insertByBo(VoteFeedbackBo bo) {
        VoteFeedback add = BeanUtil.toBean(bo, VoteFeedback.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改问题反馈
     */
    @Override
    public Boolean updateByBo(VoteFeedbackBo bo) {
        VoteFeedback update = BeanUtil.toBean(bo, VoteFeedback.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除问题反馈
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
